
const { UserRelation, User } = require('../db/model');
const { formatUser } = require('./_format');
const Sequelize = require('sequelize')

/**
 * 添加关注关系
 * @param {*} userId 用户id 
 * @param {*} followerId 背关注用户id
 */
async function addFollower( userId, followerId ) {
    const result = await UserRelation.create({
        userId,
        followerId
    })
    return result.dataValues;
}

/**
 * 删除关注关系
 * @param {*} userId 用户id
 * @param {*} followerId 被关注用户id
 */
async function deleteFollower( userId, followerId ) {
    const result = await UserRelation.destroy({
        where: {
            userId,
            followerId
        }
    })
    return result > 0;
}

/**
 * 获取关注该用户的用户列表， 即该用户的粉丝
 * @param {*} followerId 背关注人的id
 */
async function getUsersByFollower( followerId ) {
    const result = await User.findAndCountAll({
        attributes: ['id', 'userName', 'nickName', 'picture' ],
        order: [
            ['id','desc']
        ],
        include: [
            {
                model: UserRelation,
                where: {
                    followerId,
                    userId: {
                        [Sequelize.Op.ne]: followerId
                    }
                }
            }
        ]
    })

    let userList = result.rows.map( row => row.dataValues );
    userList = formatUser( userList );

    return { 
        count: result.count,
        userList
    }
}

/**
 * 获取关注人列表
 * @param {*} userId 用户id 
 */
async function getFollowersByUser( userId ) {
    const result = await UserRelation.findAndCountAll({
        order: [
            ['id','desc']
        ],
        include: [
            {
                model: User,
                attributes: ['id', 'userName', 'nickName', 'picture' ]
            }
        ],
        where: {
            userId,
            followerId: {
                [Sequelize.Op.ne]: userId
            }
        }
    })

    let userList = result.rows.map( row => row.dataValues );
    
    userList = userList.map( item => {
        let user = item.user;
        user = user.dataValues;
        user = formatUser( user );
        return user;
    })
    console.log( '剑来', userList )
    return {
        count : result.count,
        userList
    }
}

module.exports = {
    addFollower,
    deleteFollower,
    getFollowersByUser,
    getUsersByFollower
}